﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Framework.Common;

namespace System
{
    public static class ReflectExtension
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        public static Type Print(this Type t)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("名称信息：\n");
            sb.Append("Name: " + t.Name + "\n");
            sb.Append("FullName: " + t.FullName + "\n");
            sb.Append("Namespace: " + t.Namespace + "\n");

            sb.Append("\n其他信息：\n");
            sb.Append("BaseType(基类型): " + t.BaseType + "\n");
            sb.Append("UnderlyingSystemType: " + t.UnderlyingSystemType + "\n");

            sb.Append("\n类型信息：\n");
            sb.Append("Attributes(TypeAttributes位标记): " + t.Attributes + "\n");
            sb.Append("IsValueType(值类型): " + t.IsValueType + "\n");
            sb.Append("IsEnum(枚举): " + t.IsEnum + "\n");
            sb.Append("IsClass(类): " + t.IsClass + "\n");
            sb.Append("IsArray(数组): " + t.IsArray + "\n");
            sb.Append("IsInterface(接口): " + t.IsInterface + "\n");
            sb.Append("IsPointer(指针): " + t.IsPointer + "\n");
            sb.Append("IsSealed(密封): " + t.IsSealed + "\n");
            sb.Append("IsPrimitive(基类型): " + t.IsPrimitive + "\n");
            sb.Append("IsAbstract(抽象): " + t.IsAbstract + "\n");
            sb.Append("IsPublic(公开): " + t.IsPublic + "\n");
            sb.Append("IsNotPublic(不公开): " + t.IsNotPublic + "\n");
            sb.Append("IsVisible: " + t.IsVisible + "\n");
            sb.Append("IsByRef(由引用传递): " + t.IsByRef + "\n");

            #region member info
            sb.Append("查看类型 " + t.Name + "的成员信息：\n");
            sb.Append(String.Empty.PadLeft(50, '-') + "\n");

            MemberInfo[] memberInfo = t.GetMembers(BindingFlags.Public |
                                                   BindingFlags.Static |
                                                   BindingFlags.NonPublic |
                                                   BindingFlags.Instance |
                                                   BindingFlags.DeclaredOnly);
            foreach (MemberInfo mi in memberInfo)
            {
                sb.Append("成员：" + mi.ToString().PadRight(40) + " 类型: " + mi.MemberType + "\n");
            }
            #endregion

            #region field info
            sb.Append("查看类型 " + t.Name + "的字段信息：\n");
            sb.Append(String.Empty.PadLeft(50, '-') + "\n");

            FieldInfo[] fields = t.GetFields();
            foreach (FieldInfo fi in fields)
            {
                sb.Append("名称：" + fi.Name + "\n");
                sb.Append("类型：" + fi.FieldType + "\n");
                sb.Append("属性：" + fi.Attributes + "\n\n");
            }
            #endregion

            #region property info
            sb.Append("查看类型 " + t.Name + "的属性信息：\n");
            sb.Append(String.Empty.PadLeft(50, '-') + "\n");

            PropertyInfo[] properties = t.GetProperties();

            foreach (PropertyInfo pi in properties)
            {
                sb.Append("名称：" + pi.Name + "\n");
                sb.Append("类型：" + pi.PropertyType + "\n");
                sb.Append("可读：" + pi.CanRead + "\n");
                sb.Append("可写：" + pi.CanWrite + "\n");
                sb.Append("属性：" + pi.Attributes + "\n");
            }
            #endregion

            #region method info
            sb.Append("查看类型 " + t.Name + "的方法信息：\n");
            sb.Append(String.Empty.PadLeft(50, '-') + "\n");

            MethodInfo[] methods = t.GetMethods(BindingFlags.Public |
                                                BindingFlags.Static |
                                                BindingFlags.NonPublic |
                                                BindingFlags.Instance |
                                                BindingFlags.DeclaredOnly);

            foreach (MethodInfo method in methods)
            {
                sb.Append("名称：" + method.Name + "\n");
                sb.Append("签名：" + method.ToString() + "\n");
                sb.Append("属性：" + method.Attributes + "\n");
                sb.Append("返回值类型：" + method.ReturnType + "\n\n");
            }
            #endregion

            LogHelper.Debug(sb);
            return t;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="asm"></param>
        /// <returns></returns>
        public static Assembly Print(this Assembly asm)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("FullName(全名)：" + asm.FullName + "\n");
            sb.Append("Location(路径)：" + asm.Location + "\n");
            Type[] types = asm.GetTypes();
            foreach (Type t in types)
            {
                sb.Append("   类型：" + t + "\n");
            }
            LogHelper.Debug(sb);
            return asm;
        }
    }
}
